home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 28.6 KB | 999 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWEdView.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWFrameW.hpp"
-
- #ifndef FWEDVIEW_H
- #include "FWEdView.h"
- #endif
-
- #ifndef FWFRAME_H
- #include "FWFrame.h"
- #endif
-
- #ifndef FWPART_H
- #include "FWPart.h"
- #endif
-
- #ifndef FWDIALOG_H
- #include "FWDialog.h"
- #endif
-
- #ifndef FWEVENT_H
- #include "FWEvent.h"
- #endif
-
- #ifndef FWMNUBAR_H
- #include "FWMnuBar.h"
- #endif
-
- #ifndef FWRECSHP_H
- #include "FWRecShp.h"
- #endif
-
- #ifndef FWSELECT_H
- #include "FWSelect.h"
- #endif
-
- #ifndef FWODGEOM_H
- #include "FWODGeom.h"
- #endif
-
- #ifndef FWWINDOW_H
- #include "FWWindow.h"
- #endif
-
- #ifndef FWCURSOR_H
- #include "FWCursor.h"
- #endif
-
- #ifndef FWFONT_H
- #include "FWFont.h"
- #endif
-
- #ifndef FWEVEDEF_H
- #include "FWEveDef.h"
- #endif
-
- #ifndef FWCONTXT_H
- #include "FWContxt.h"
- #endif
-
- #ifndef FWMEMORY_H
- #include "FWMemory.h"
- #endif
-
- #ifndef SOM_ODFrame_xh
- #include <Frame.xh>
- #endif
-
- #ifndef SOM_ODShape_xh
- #include <Shape.xh>
- #endif
-
- #ifndef SLMIXOS_H
- #include <SLMixOS.h> // for FW_Beep
- #endif
-
- #ifndef SOM_Module_OpenDoc_Errors_defined
- #include <ErrorDef.xh>
- #endif
-
- #ifdef FW_BUILD_MAC
- #include <scrap.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
- #include <Script.h>
- #endif
-
- //========================================================================================
- // Runtime Information
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment fwgadgts
- #endif
-
- FW_DEFINE_CLASS_M2(FW_CEditView, FW_CView, FW_MNotifier)
- FW_DEFINE_AUTO(FW_CEditView)
-
- //========================================================================================
- // Utilities
- //========================================================================================
-
- inline Rect& operator+= (Rect & r, Point p)
- {
- r.top += p.v;
- r.left += p.h;
- r.bottom += p.v;
- r.right += p.h;
- return r;
- }
-
- #define FW_ISBACKSPACE(c) (c == 0x08)
- #define FW_ISFWDDELETE(c) (c == 0x7f)
- #define FW_ISARROWKEY(c) (c == 0x1c || c == 0x1d || c == 0x1e || c == 0x1f)
- #define FW_ISINPUTCHAR(c) !FW_ISBACKSPACE(c) && !FW_ISFWDDELETE(c) && !FW_ISARROWKEY(c)
-
- //========================================================================================
- // CLASS FW_CEditView
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::FW_CEditView
- //----------------------------------------------------------------------------------------
-
- FW_CEditView::FW_CEditView (Environment* ev,
- FW_CSuperView* container,
- const FW_CRect& bounds,
- ODID viewID,
- const FW_CString& str,
- const FW_CFont& font,
- short maxChars,
- unsigned short attributes)
- : FW_CView (ev, container, bounds, viewID),
- fMacTEHandle (nil),
- fMaxChars(maxChars),
- fEditAttributes (attributes)
- {
- Initialize(ev, str, font);
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::FW_CEditView
- //----------------------------------------------------------------------------------------
-
- FW_CEditView::FW_CEditView (Environment* ev,
- FW_CSuperView* container,
- const FW_CRect& bounds,
- ODID viewID)
- : FW_CView (ev, container, bounds, viewID),
- FW_MNotifier(),
- fMacTEHandle (nil),
- fMaxChars(FW_MAXINT16),
- fEditAttributes (FW_CEditView::kDrawBox + FW_CEditView::kAutoScroll)
- {
- Initialize(ev, FW_CString(), FW_kNormalFont);
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::FW_CEditView
- //----------------------------------------------------------------------------------------
-
- FW_CEditView::FW_CEditView (Environment* ev)
- : FW_CView (ev),
- FW_MNotifier(),
- fMacTEHandle (nil),
- fMaxChars(0),
- fEditAttributes(0)
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::InitEditView
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::Initialize(Environment* ev, const FW_CString& text, const FW_CFont& font)
- {
- // Set up the current port for the TE record - [LSD] OK to use active facet?
- ODFacet* facet = GetFrame(ev)->GetActiveFacet(ev);
- FW_ASSERT(facet);
- FW_CPlatformGraphicContext gc (ev, facet);
-
- FW_CPlatformRect pBounds;
- fMacTEHandle = ::TENew (&pBounds, &pBounds);
- if (!fMacTEHandle)
- FW_Failure (kODErrOutOfMemory);
-
- SetFont(ev, font);
-
- // Set TE feature flags
- ::TEFeatureFlag(teFAutoScr, (fEditAttributes & FW_CEditView::kAutoScroll) ?
- teBitSet : teBitClear, fMacTEHandle);
- ::TEFeatureFlag(teFTextBuffering, (fEditAttributes & FW_CEditView::kTextBuffering) ?
- teBitSet : teBitClear, fMacTEHandle);
- ::TEFeatureFlag(teFOutlineHilite, (fEditAttributes & FW_CEditView::kOutlineHilite) ?
- teBitSet : teBitClear, fMacTEHandle);
- ::TEFeatureFlag(teFInlineInput, (fEditAttributes & FW_CEditView::kInlineInput) ?
- teBitSet : teBitClear, fMacTEHandle);
- ::TEFeatureFlag(teFUseTextServices, (fEditAttributes & FW_CEditView::kTextServices) ?
- teBitSet : teBitClear, fMacTEHandle);
-
- // Mac TextEdit is limited to 32K
- if (fMaxChars <= 0 || fMaxChars > FW_MAXINT16)
- fMaxChars = FW_MAXINT16;
-
- // Access the string buffer directly to pass the char* to TEInsert
- FW_ByteCount bufLen = text.GetByteLength();
- if (bufLen > 0)
- {
- if (bufLen > fMaxChars)
- bufLen = fMaxChars;
-
- const char* buf = text.RevealBuffer();
- ::TESetText(buf, bufLen, fMacTEHandle);
- }
-
- // Update TE rects and recompute text layout
- MacSetRects(ev);
-
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::~FW_CEditView
- //----------------------------------------------------------------------------------------
-
- FW_CEditView::~FW_CEditView ()
- {
- FW_START_DESTRUCTOR
- if (fMacTEHandle)
- {
- // XXX need to test disposing when the window is still up
- ::TEDispose (fMacTEHandle);
- }
- }
-
- #pragma mark -
- //----------------------------------------------------------------------------------------
- // FW_CEditView::GetTextLength
- //----------------------------------------------------------------------------------------
-
- FW_ByteCount FW_CEditView::GetTextLength (Environment* ev) const
- {
- FW_UNUSED(ev);
-
- #if defined(FW_BUILD_MAC)
- return (FW_ByteCount)(*MacTE())->teLength;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::GetText
- //----------------------------------------------------------------------------------------
-
- FW_CString FW_CEditView::GetText (Environment* ev) const
- {
- FW_UNUSED(ev);
-
- #if defined(FW_BUILD_MAC)
- Handle handleText = (*MacTE())->hText;
- FW_Locale locale;
- GetLocale(ev, locale);
-
- FW_CAcquireLockedSystemHandle locked (handleText);
- return FW_CString(*handleText, (FW_ByteCount)(*MacTE())->teLength, locale);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::GetSelectedText
- //----------------------------------------------------------------------------------------
-
- FW_CString FW_CEditView::GetSelectedText (Environment* ev) const
- {
- FW_UNUSED(ev);
-
- #if defined(FW_BUILD_MAC)
- short startOffset = (*MacTE())->selStart;
- FW_ByteCount byteCount = (*MacTE())->selEnd - startOffset;
- FW_Locale locale;
- GetLocale(ev, locale);
-
- if (byteCount <= 0) // empty selection
- {
- FW_CString emptyString;
- emptyString.SetEmpty(locale);
- return FW_CString(emptyString);
- }
-
- Handle handleText = (*MacTE())->hText;
- FW_CAcquireLockedSystemHandle locked (handleText);
- return FW_CString(*handleText + startOffset, byteCount, locale);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::GetSelectionRange
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::GetSelectionRange (Environment* ev, short& startOffset, short& endOffset) const
- {
- FW_UNUSED(ev);
-
- #if defined(FW_BUILD_MAC)
- startOffset = (*MacTE())->selStart;
- endOffset = (*MacTE())->selEnd;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::ClearText
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::ClearText (Environment* ev)
- {
- SetText (ev, FW_CString());
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::SetText
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::SetText (Environment* ev, const FW_CString& str)
- {
- #if defined(FW_BUILD_MAC)
- // Access the string buffer directly to pass the char* to TEInsert
- FW_ByteCount bufLen = str.GetByteLength();
- if (bufLen > fMaxChars)
- bufLen = fMaxChars;
- const char* buf = str.RevealBuffer();
-
- ODFacet* facet = GetFrame(ev)->GetActiveFacet(ev);
- if (facet == NULL)
- return; // frame is in limbo
-
- FW_CViewContext gc(ev, this, facet);
- MacAdjustRects(ev, gc);
-
- PrivSelectAll();
- ::TEDelete (fMacTEHandle);
- ::TEInsert (buf, bufLen, fMacTEHandle);
- #endif
- Notify(ev, FW_kChangedMsg);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::SetSelectedText
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::SetSelectedText(Environment* ev, const FW_CString& newText)
- {
- #if defined(FW_BUILD_MAC)
- FW_ByteCount bufLen = newText.GetByteLength();
- const char* buf = newText.RevealBuffer();
-
- // Don't insert more characters than allowed
- FW_ByteCount notSelectedChars = (*MacTE())->teLength - ((*MacTE())->selEnd - (*MacTE())->selStart);
-
- if (notSelectedChars >= fMaxChars)
- return;
- if ((bufLen + notSelectedChars) > fMaxChars)
- bufLen = fMaxChars - notSelectedChars;
-
- ODFacet* facet = GetFrame(ev)->GetActiveFacet(ev);
- if (facet == NULL)
- return; // frame is in limbo
-
- FW_CViewContext gc(ev, this, facet);
- MacAdjustRects(ev, gc);
-
- ::TEDelete (fMacTEHandle); // delete selected text
- ::TEInsert (buf, bufLen, fMacTEHandle); // insert new text
- #endif
- Notify(ev, FW_kChangedMsg);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::InsertText
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::InsertText(Environment* ev, const FW_CString& textToInsert, short offset)
- {
- #ifdef FW_BUILD_MAC
-
- FW_ByteCount bufLen = textToInsert.GetByteLength();
- const char* buf = textToInsert.RevealBuffer();
-
- // Don't insert more characters than allowed
- FW_ByteCount curLength = (*MacTE())->teLength;
- if (curLength >= fMaxChars)
- return;
- if ((bufLen + curLength) > fMaxChars)
- bufLen = fMaxChars - curLength;
-
- ODFacet* facet = GetFrame(ev)->GetActiveFacet(ev);
- if (facet == NULL)
- return; // frame is in limbo
-
- FW_CViewContext gc(ev, this, facet);
- MacAdjustRects(ev, gc);
-
- ::TESetSelect(offset, offset, fMacTEHandle);
- ::TEInsert(buf, bufLen, fMacTEHandle);
- #endif
- Notify(ev, FW_kChangedMsg);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::SelectText
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::SelectText(Environment* ev, short startOffset, short endOffset)
- {
- #ifdef FW_BUILD_MAC
- short offset = startOffset;
- if (startOffset > endOffset)
- {
- startOffset = endOffset;
- endOffset = offset;
- }
-
- ODFacet* facet = GetFrame(ev)->GetActiveFacet(ev);
- if (facet == NULL)
- return; // frame is in limbo
-
- FW_CViewContext gc(ev, this, facet);
- MacAdjustRects(ev, gc);
-
- ::TESetSelect(startOffset, endOffset, fMacTEHandle);
- #endif
- Notify(ev, FW_kChangedMsg);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::PrivSelectAll
- //----------------------------------------------------------------------------------------
- // Used internally when we don't need to create a view context
-
- void FW_CEditView::PrivSelectAll()
- {
- #ifdef FW_BUILD_MAC
- ::TESetSelect (0, FW_MAXINT16, fMacTEHandle);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::PrivCheckMaxChars
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CEditView::PrivCheckMaxChars(long numChars)
- {
- // checks if we are trying to add too many chars
- #ifdef FW_BUILD_MAC
- long numSelectedChars = (*MacTE())->selEnd - (*MacTE())->selStart;
-
- return (((*MacTE())->teLength + numChars - numSelectedChars) > fMaxChars);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::SetFont
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::SetFont (Environment* ev, const FW_CFont &font)
- {
- FW_UNUSED(ev);
- #ifdef FW_BUILD_MAC
- // if textSize is less than zero, we've somehow gotten
- // a styled text record, which we don't expect.
- FW_ASSERT((*MacTE())->txSize >= 0);
-
- (*MacTE())->txFont = font.MacGetFontID();
- (*MacTE())->txFace = font.MacGetFontStyle();
- (*MacTE())->txSize = FW_FixedToInt(font.GetFontSize());
- // (*MacTE())->txMode = scrOr;
-
- FMInput fontSpecs;
- fontSpecs.family = (*MacTE())->txFont;
- fontSpecs.size = (*MacTE())->txSize;
- fontSpecs.face = (*MacTE())->txFace;
- fontSpecs.needBits = false;
- fontSpecs.device = 0;
- fontSpecs.numer.h = 1;
- fontSpecs.numer.v = 1;
- fontSpecs.denom.h = 1;
- fontSpecs.denom.v = 1;
-
- FMOutPtr fontInfo = ::FMSwapFont(&fontSpecs);
- (*MacTE())->lineHeight = fontInfo->ascent + fontInfo->descent + fontInfo->leading;
- (*MacTE())->fontAscent = fontInfo->ascent;
- ::TECalText(fMacTEHandle);
-
- #endif
- Notify(ev, FW_kChangedMsg);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::GetFont
- //----------------------------------------------------------------------------------------
-
- FW_CFont FW_CEditView::GetFont(Environment* ev) const
- {
- FW_UNUSED(ev);
- #ifdef FW_BUILD_MAC
- FW_CFont font;
-
- font.SetFontStyle(FW_CFont::PlatformToODFFontStyle((*MacTE())->txFace));
- font.MacSetFontID((*MacTE())->txFont);
- font.SetFontSize(FW_IntToFixed((*MacTE())->txSize));
-
- return font;
- #endif
- }
-
- #pragma mark -
- //----------------------------------------------------------------------------------------
- // FW_CEditView::SetVisible
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::SetVisible(Environment* ev, FW_Boolean visible, FW_Boolean propagate)
- {
- FW_CView::SetVisible(ev, visible, propagate);
-
- if (!visible)
- {
- // Will stop idling if this edit view was active
- ResignTarget(ev);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::WantsToBeTarget
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CEditView::WantsToBeTarget(Environment* ev)
- {
- // Let visible edit views process keys and menu commands
- if (IsVisible(ev))
- return TRUE;
- else
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::ActivateTarget
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::ActivateTarget(Environment* ev, FW_Boolean tabSelection)
- {
- FW_UNUSED(ev);
- ODFacet* facet = GetFrame(ev)->GetActiveFacet(ev);
- FW_ASSERT(facet);
-
- FW_CViewContext gc(ev, this, facet);
- MacAdjustRects(ev, gc);
-
- if (tabSelection)
- PrivSelectAll();
-
- ::TEActivate (fMacTEHandle);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::DeactivateTarget
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::DeactivateTarget (Environment* ev)
- {
- ODFacet* facet = GetFrame(ev)->GetActiveFacet(ev);
- if (facet)
- {
- FW_CViewContext gc(ev, this, facet);
- MacAdjustRects(ev, gc);
-
- ::TEDeactivate(fMacTEHandle);
- }
-
- PrivEnableEditMenu(ev, false);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::PrivEnableEditMenu
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::PrivEnableEditMenu(Environment *ev, FW_Boolean enable)
- {
- // Check if this edit-view belongs to a Modal Dialog before enabling the Edit menu
- FW_CFrame* frame = GetFrame(ev);
- if (GetFrame(ev)->HasModalFocus(ev))
- frame->GetPart(ev)->GetMenuBar(ev)->EnableCommand(ev, kODCommandEditMenu, enable);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::DoIdle
- //----------------------------------------------------------------------------------------
-
- FW_Handled FW_CEditView::DoIdle (Environment* ev, const FW_CNullEvent& event)
- {
- FW_UNUSED(event);
- ODFacet* facet = GetFrame(ev)->GetActiveFacet(ev);
-
- // facet is NULL when frame is in limbo
- if (facet)
- {
- FW_CViewContext gc(ev, this, facet);
- MacAdjustRects(ev, gc);
-
- #if defined(FW_BUILD_MAC)
- ::TEIdle (fMacTEHandle);
- #endif
- }
-
- return FW_kNotHandled;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::DoMouseDown
- //----------------------------------------------------------------------------------------
-
- FW_Handled FW_CEditView::DoMouseDown (Environment* ev, const FW_CMouseEvent& event)
- {
- FW_CViewContext gc(ev, this, event.GetFacet(ev));
- MacAdjustRects(ev, gc);
-
- FW_CPoint where = event.GetMousePosition (ev, FW_CMouseEvent::kFrame);
- FrameToViewContent (ev, where);
- FW_CPlatformPoint pWhere;
- pWhere = gc.LogicalToDevice (where);
-
- ::TEClick (pWhere, event.IsExtendModifier(ev), fMacTEHandle);
-
- return FW_kHandled;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::DoVirtualKey
- //----------------------------------------------------------------------------------------
-
- FW_Handled FW_CEditView::DoVirtualKey (Environment* ev, const FW_CVirtualKeyEvent & event)
- {
- FW_Handled keyHandled = FW_kNotHandled;
- short keyCode = event.GetKeyCode(ev);
-
- if (keyCode == FW_kVKHome || keyCode == FW_kVKEnd)
- {
- keyHandled = FW_kHandled;
-
- FW_CViewContext gc(ev, this, GetFrame(ev)->GetActiveFacet(ev));
- MacAdjustRects(ev, gc);
-
- if(keyCode == FW_kVKHome)
- ::TESetSelect(0, 0, fMacTEHandle); // Go to the top
- else
- ::TESetSelect(FW_MAXINT16, FW_MAXINT16, fMacTEHandle); // Go to the end
- }
-
- return keyHandled; // if false key will be handled in DoCharKey
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::DoCharKey
- //----------------------------------------------------------------------------------------
-
- FW_Handled FW_CEditView::DoCharKey (Environment* ev, const FW_CCharKeyEvent & event)
- {
- // Ignore Command-characters which are not associated to menu commands
- if (event.GetPlatformModifiers(ev) & cmdKey)
- return FW_kNotHandled;
-
- return InputCharKey(ev, event.GetChar(ev));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::InputCharKey
- //----------------------------------------------------------------------------------------
-
- FW_Handled FW_CEditView::InputCharKey (Environment* ev, char c)
- {
- FW_CViewContext gc(ev, this, GetFrame(ev)->GetActiveFacet(ev));
- MacAdjustRects(ev, gc);
-
- #if defined(FW_BUILD_MAC)
- if (FW_ISINPUTCHAR(c) && PrivCheckMaxChars(1) == true)
- FW_Beep();
- else
- {
- ::TEKey (c, fMacTEHandle);
- Notify(ev, FW_kChangedMsg);
- }
-
- #endif
-
- return FW_kHandled;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::DoAdjustMenus
- //----------------------------------------------------------------------------------------
-
- FW_Handled FW_CEditView::DoAdjustMenus (Environment *ev, FW_CMenuBar* menuBar,
- FW_Boolean hasMenuFocus, FW_Boolean isRoot)
- {
- FW_UNUSED(isRoot);
-
- if (hasMenuFocus)
- {
- // Must adjust edit menu for modal dialogs first
- PrivEnableEditMenu(ev, true);
-
- long offset;
- FW_Boolean hasSelection = ((*MacTE())->selStart != (*MacTE())->selEnd);
- FW_Boolean hasCharacters = ((*MacTE())->teLength > 0);
- FW_Boolean hasClipboard = (::GetScrap(0, 'TEXT', &offset) > 0);
-
- menuBar->EnableCommand (ev, kODCommandCut, hasSelection);
- menuBar->EnableCommand (ev, kODCommandCopy, hasSelection);
- menuBar->EnableCommand (ev, kODCommandPaste, hasClipboard);
- menuBar->EnableCommand (ev, kODCommandClear, hasSelection);
- menuBar->EnableCommand (ev, kODCommandSelectAll, hasCharacters);
- }
-
- return FW_kNotHandled; // let parent views adjust their menus
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::DoTECommand
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::DoTECommand (Environment* ev, ODCommandID id, FW_Boolean useScrap)
- {
- // DoTECommand can be called by subclasses with useScrap = false when implementing
- // support for ODF Data Interchange (see the Form example)
-
- ODFacet* facet = GetFrame(ev)->GetActiveFacet(ev);
- FW_ASSERT(facet);
- FW_CViewContext gc(ev, this, facet);
- MacAdjustRects(ev, gc);
-
- switch (id)
- {
- case kODCommandCut:
- ::TECut (fMacTEHandle);
- if (useScrap)
- {
- ::ZeroScrap();
- ::TEToScrap();
- }
- break;
-
- case kODCommandCopy:
- ::TECopy (fMacTEHandle);
- if (useScrap)
- {
- ::ZeroScrap();
- ::TEToScrap();
- }
- break;
-
- case kODCommandPaste:
- if (useScrap)
- {
- long offset;
- long scrapLength = ::GetScrap(0, 'TEXT', &offset);
- if (PrivCheckMaxChars(scrapLength))
- {
- FW_Beep(); // can't paste that many characters
- break;
- }
- ::TEFromScrap();
- }
- ::TEPaste (fMacTEHandle);
- Notify(ev, FW_kChangedMsg);
- break;
-
- case kODCommandClear:
- Notify(ev, FW_kChangedMsg);
- ::TEDelete (fMacTEHandle);
- break;
-
- case kODCommandSelectAll:
- PrivSelectAll();
- break;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::DoMenu
- //----------------------------------------------------------------------------------------
-
- FW_Handled FW_CEditView::DoMenu (Environment* ev, const FW_CMenuEvent& event)
- {
- ODCommandID id = event.GetCommandID(ev);
-
- switch (id)
- {
- case kODCommandCut:
- case kODCommandCopy:
- case kODCommandPaste:
- case kODCommandClear:
- case kODCommandSelectAll:
- DoTECommand(ev, id, true);
- break;
-
- default:
- return FW_CView::DoMenu (ev, event);
- }
- return FW_kHandled;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::Draw
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::Draw (Environment *ev, ODFacet* facet, ODShape* invalidShape)
- {
- FW_UNUSED(invalidShape);
- if (HasBox()) {
- // don't use a platform context for the adorners
- FW_CViewContext gc (ev, this, facet);
- FW_CRect bounds(FW_kZeroPoint, GetSize(ev));
- FW_CRectShape::RenderRect (gc, bounds, FW_kFrame, FW_CInk(FW_kRGBBlack));
-
- // [LSD] Need this?
- bounds.Inset (FW_kFixedPos1, FW_kFixedPos1);
- FW_CRectShape::RenderRect (gc, bounds, FW_kFill, FW_kWhiteEraseInk);
- }
- FW_CViewContext gc(ev, this, facet);
- MacAdjustRects(ev, gc);
- FW_CPlatformRect pBounds = gc.LogicalToDevice (GetTextBounds(ev));
-
- #if defined(FW_BUILD_MAC)
- ::TEUpdate (&pBounds, fMacTEHandle);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::AdjustCursor
- //----------------------------------------------------------------------------------------
-
- FW_Handled FW_CEditView::AdjustCursor(Environment *ev, ODFacet* odFacet, const FW_CPoint& where, ODEventInfo* eventInfo)
- {
- FW_UNUSED(odFacet);
- FW_UNUSED(where);
- FW_UNUSED(eventInfo);
-
- if (GetFrame(ev)->HasSelectionFocus(ev) == FALSE)
- return FW_kNotHandled;
-
- FW_gIBeamCursor.Select();
- return FW_kHandled;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::LocationChanged
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::LocationChanged(Environment* ev, const FW_CPoint& oldLocation)
- {
- FW_CView::LocationChanged(ev, oldLocation);
-
- // call MacAdjustRects() instead of MacSetRects() since size didn't change
- FW_CViewContext gc(ev, this, GetFrame(ev)->GetActiveFacet(ev));
- MacAdjustRects(ev, gc);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::SizeChanged
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::SizeChanged(Environment* ev, const FW_CPoint& oldSize)
- {
- FW_CView::SizeChanged(ev, oldSize);
-
- MacSetRects (ev);
- }
-
- #pragma mark -
- //----------------------------------------------------------------------------------------
- // FW_CEditView::GetTextBounds
- //----------------------------------------------------------------------------------------
-
- FW_CRect FW_CEditView::GetTextBounds (Environment* ev)
- {
- // Get view bounds
- FW_CRect bounds (FW_kZeroPoint, GetSize(ev));
-
- // shrink by 2 pixels if the edit view has a box
- if (HasBox())
- bounds.Inset(FW_IntToFixed(2), FW_IntToFixed(2));
-
- return bounds;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::MacAdjustRects
- //----------------------------------------------------------------------------------------
- // MacAdjustRects aligns the TEhandle's rect fields to the view (which may have
- // scrolled, etc.). Don't need to call TECalText here, rect sizes are not
- // changed.
-
- void FW_CEditView::MacAdjustRects (Environment* ev, FW_CGraphicContext & gc)
- {
- FW_CPlatformRect pBounds = gc.LogicalToDevice (GetTextBounds(ev));
-
- if (pBounds != (*MacTE())->viewRect) {
- FW_CPlatformPoint moving (pBounds.left - (*MacTE())->viewRect.left, pBounds.top - (*MacTE())->viewRect.top);
- (*MacTE())->viewRect += moving;
- (*MacTE())->destRect += moving;
- (*MacTE())->selRect += moving;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::MacSetRects
- //----------------------------------------------------------------------------------------
- // MacSetRects resets the TEhandle's rect field at init time or after the view
- // has been resized.
-
- void FW_CEditView::MacSetRects (Environment* ev)
- {
- FW_CViewContext gc (ev, this, GetFrame(ev)->GetActiveFacet(ev));
- FW_CPlatformRect pBounds = gc.LogicalToDevice (GetTextBounds(ev));
-
- (*MacTE())->viewRect = pBounds;
- (*MacTE())->destRect = pBounds;
-
- // extend the destination rectangle far to the right to avoid word wrapping
- // [LSD] this doesn't work with center or right justification
- if (HasWordWrap() == false)
- (*MacTE())->destRect.right += 2000;
-
- // Let text-edit adjust line breaks
- ::TECalText (fMacTEHandle);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::Flatten
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::Flatten(Environment* ev, FW_CWritableStream& stream) const
- {
- FW_CView::Flatten(ev, stream);
-
- stream << fMaxChars;
- stream << fEditAttributes;
- stream << GetFont(ev);
- stream << GetText(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::InitializeFromStream
- //----------------------------------------------------------------------------------------
-
- void FW_CEditView::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
- {
- FW_CView::InitializeFromStream(ev, stream);
-
- FW_CString text;
- FW_CFont font;
-
- stream >> fMaxChars;
- stream >> fEditAttributes;
- stream >> font;
- stream >> text;
-
- Initialize(ev, text, font);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CEditView::GetLocale
- //----------------------------------------------------------------------------------------
- void FW_CEditView::GetLocale(Environment* ev, FW_Locale& locale) const
- {
- FW_UNUSED(ev);
-
- #ifdef FW_BUILD_MAC
- short fontNumber = (*MacTE())->txFont;
- locale.fScriptCode = ::FontToScript(fontNumber); // Get the script from the font
- locale.fLangCode = ::GetScriptVariable(locale.fScriptCode, smScriptLang); // Get the language from the script
- #endif
-
- #ifdef FW_BUILD_WIN
- // Not yet implemented
- locale.fScriptCode = 0;
- locale.fLangCode = 0;
- #endif
- }
-